/**
 * $Id$ Copyright 2009-2012 Oak Pacific Interactive. All rights reserved.
 */
package com.shuqi.common.utils;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.shuqi.application.ShuqiApplication;
import com.shuqi.controller.R;

/**
 * 
 * 
 * @author <a href="mailto:">yaozhen</a>
 * @version 2014-2-26上午11:05:54
 */
public class CheckInputUtils {

    public static boolean checkEmail(String email, TextView tv) {
        if (TextUtils.isEmpty(email) || TextUtils.isEmpty(email.trim())) {
            showText(tv, R.string.email_empty);
            return false;
        }
        boolean isEmail = RegexUtil.isEmail(email);
        if (isEmail) {
            tv.setVisibility(View.INVISIBLE);
        }else{
            showText(tv, R.string.email_error);
        }
        return isEmail;
    }

    private static void showText(TextView tv, int sId) {
        if (tv != null) {
            tv.setVisibility(View.VISIBLE);
            tv.setTextColor(ShuqiApplication.getContext().getResources()
                    .getColor(R.color.hint_error_color));
            tv.setText(ShuqiApplication.getContext().getString(sId));
        }
    }

    public static boolean checkLoginSettingPassword(String pwd, TextView tv) {
        if (TextUtils.isEmpty(pwd) || TextUtils.isEmpty(pwd.trim())) {
            showText(tv, R.string.password_login_setting_empty);
            return false;
        }

        return checkCommonRulePassword(pwd.trim(), tv);
    }

    public static boolean checkPassword(String pwd, TextView tv) {
        if (TextUtils.isEmpty(pwd) || TextUtils.isEmpty(pwd.trim())) {
            showText(tv, R.string.password_empty);
            return false;
        }

        return checkCommonRulePassword(pwd.trim(), tv);
    }

    private static boolean checkCommonRulePassword(String pwd, TextView tv) {
        if (pwd.length() < 6) {
            showText(tv, R.string.password_too_short);
            return false;
        } else if (pwd.length() > 16) {
            showText(tv, R.string.password_too_long);
            return false;
        }
        boolean isPwd = RegexUtil.isPassword(pwd);
        if (!isPwd) {
            showText(tv, R.string.password_not_right);
        }
        return isPwd;
    }

    public static boolean checkMobile(String mobile, TextView tv) {
        if (TextUtils.isEmpty(mobile) || TextUtils.isEmpty(mobile.trim())) {
            showText(tv, R.string.mobile_empty);
            return false;
        }
        boolean isMobile = RegexUtil.isMobileNO(mobile);
        if (!isMobile) {
            showText(tv, R.string.mobile_error);
        }
        return isMobile;

    }

    public static boolean checkVerifyCode(String verifyCode, TextView tv) {
        if (TextUtils.isEmpty(verifyCode) || TextUtils.isEmpty(verifyCode.trim())) {
            showText(tv, R.string.verify_code_empty);
            return false;
        }
        return true;

    }
}
